package com.hubei.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hubei.car.entity.MsNews;
import com.hubei.car.entity.MsNewsType;
import com.hubei.car.entity.PageResult;
import com.hubei.car.entity.Result;
import com.hubei.car.mapper.MsNewsMapper;
import com.hubei.car.mapper.MsNewsTypeMapper;
import com.hubei.car.service.MsNewsService;
import com.hubei.car.swaggerParam.MsNewsQueryParam;
import com.hubei.car.swaggerParam.MsNewsQueryParam2;
import com.hubei.car.swaggerParam.MsNewsTypeQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MsNewsServiceImpl implements MsNewsService {

    @Autowired
    private MsNewsMapper msNewsMapper;
    @Autowired
    private MsNewsTypeMapper msNewsTypeMapper;

    @Override
    public Result queryMsNewsTypePage(MsNewsTypeQueryParam msNewsTypeQueryParam) {
        //分页
        Page<MsNewsType> page = new Page<>(msNewsTypeQueryParam.getCurrent(), msNewsTypeQueryParam.getSize());
        //查询条件
        QueryWrapper<MsNewsType> querywrapper = new QueryWrapper<>();
        IPage<MsNewsType> msNewsTypeIPage = msNewsTypeMapper.selectPage(page, querywrapper);
        return Result.ok(new PageResult(msNewsTypeQueryParam.getCurrent(), msNewsTypeQueryParam.getSize(), msNewsTypeIPage.getTotal(), msNewsTypeIPage.getRecords()));
    }

    @Override
    public Result queryMsNewsPage(MsNewsQueryParam msNewsQueryParam) {
        //分页
        Page<MsNews> page = new Page<>(msNewsQueryParam.getCurrent(), msNewsQueryParam.getSize());
        IPage<MsNews> msNewsIPage = msNewsMapper.queryMsNewsPage(page, msNewsQueryParam);
        if (msNewsIPage == null || msNewsIPage.getTotal() == 0) {
            return Result.ok(new PageResult(msNewsQueryParam.getCurrent(), msNewsQueryParam.getSize(), 0L, null));
        }
        return Result.ok(new PageResult(msNewsQueryParam.getCurrent(), msNewsQueryParam.getSize(), msNewsIPage.getTotal(), msNewsIPage.getRecords()));
    }

    @Override
    public Result queryMsNewsPage2(MsNewsQueryParam2 msNewsQueryParam2) {
        //分页
        Page<MsNews> page = new Page<>(msNewsQueryParam2.getCurrent(), msNewsQueryParam2.getSize());
        //查询条件
        QueryWrapper<MsNews> querywrapper = new QueryWrapper<>();
        if (msNewsQueryParam2.getTid() != null) {
            querywrapper.eq("tid", msNewsQueryParam2.getTid());
        }
        page.setDesc("createdate");
        IPage<MsNews> msNewsIPage = msNewsMapper.selectPage(page, querywrapper);
        if (msNewsIPage == null) {
            return Result.ok(new PageResult(msNewsQueryParam2.getCurrent(), msNewsQueryParam2.getSize(), 0L, null));
        }
        return Result.ok(new PageResult(msNewsQueryParam2.getCurrent(), msNewsQueryParam2.getSize(), msNewsIPage.getTotal(), msNewsIPage.getRecords()));
    }

    @Override
    public Result queryAllMsNewsTypePage() {
        return Result.ok(msNewsTypeMapper.selectList(null));
    }
}
